package org.ws.lifebacklog.activity;

import java.util.ArrayList;
import java.util.List;

import org.ws.lifebacklog.R;
import org.ws.lifebacklog.adapter.GenericAdapter;
import org.ws.lifebacklog.adapter.ViewBinder;
import org.ws.lifebacklog.model.Task;
import org.ws.lifebacklog.service.BacklogService;
import org.ws.lifebacklog.util.UIUtil;
import org.ws.lifebacklog.view.IndicatorView;
import org.ws.lifebacklog.view.IndicatorView.Indicator;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

public class LifeBacklogActivity extends GenericListActivity<Task> {
	private ProgressDialog progressDialog = null;
	private List<Indicator> indicators = new ArrayList<IndicatorView.Indicator>();
	private ViewBinder<Task> viewBinder = new ViewBinder<Task>() {

		@Override
		public int getViewLayoutId() {
			return R.layout.list_item_task;
		}

		@Override
		public void bindView(View view, Task item, int position) {
			UIUtil.setText(view, R.id.li_task_title, item.getTitle());
			UIUtil.setText(view, R.id.li_task_description, "dasdasdas");
			IndicatorView iv = (IndicatorView) view.findViewById(R.id.li_task_indicator);
			iv.setIndicators(indicators);
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		startActivity(MainTabActivity.class);

		setContentView(R.layout.task_list);

		indicators.add(new Indicator(R.color.indicator_color_green, 4));
		indicators.add(new Indicator(R.color.indicator_color_red, 5));

		progressDialog = ProgressDialog.show(this, "Loading", "Please wait while task are loading...");
		progressDialog.show();
		new AsyncTask<Void, Void, List<Task>>() {

			@Override
			protected List<Task> doInBackground(Void... params) {
				return BacklogService.getInstance().getTasks();
			}

			/** {@inheritDoc} */
			@Override
			protected void onPostExecute(List<Task> result) {
				setListAdapter(new GenericAdapter<Task>(LifeBacklogActivity.this, viewBinder, result));
				progressDialog.dismiss();
			}
		}.execute();
	}

	/** {@inheritDoc} */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		getApplication().onTerminate();

	}
}